function [counts] = nearest(k)
    [zero one two three four five six seven eight nine ...
    zero_t one_t two_t three_t four_t five_t six_t seven_t eight_t nine_t test training] = parsedata();
    distances = zeros(size(training,1),2);
    counts = zeros(1,11);

    for i=1:size(zero_t,1)
        % create some (label,distance) pairs
        
        %the first column is the labels
        distances(:,1) = training(:,1);
        % run the distance formula on the remaining columns in each row
        distances(:,2) = distance(training(:,2:end), zero_t(i,:));
        distances = sortrows(distances,2);
        
        % run a tiebreaker for the kNN
        if(getlabel(distances(1:k,:)) == 0)
            counts(1) = counts(1) + 1;
        end
    end

    counts(11) = counts(1) + counts(11);
    counts(1) = (1 - counts(1)/size(zero_t,1));
    
    for i=1:size(one_t,1)
        distances(:,1) = training(:,1);
        distances(:,2) = distance(training(:,2:end), one_t(i,:));
        distances = sortrows(distances,2);
        
        if(getlabel(distances(1:k,:)) == 1)
            counts(2) = counts(2) + 1;
        end
    end
    counts(11) = counts(2) + counts(11);
    counts(2) = (1 - counts(2)/size(one_t,1));

    for i=1:size(two_t,1)
        distances(:,1) = training(:,1);
        distances(:,2) = distance(training(:,2:end), two_t(i,:));
        distances = sortrows(distances,2);
        
        if(getlabel(distances(1:k,:)) == 2)
            counts(3) = counts(3) + 1;
        end
    end
    counts(11) = counts(3) + counts(11);
    counts(3) = (1 - counts(3)/size(two_t,1));

    for i=1:size(three_t,1)
        distances(:,1) = training(:,1);
        distances(:,2) = distance(training(:,2:end), three_t(i,:));
        distances = sortrows(distances,2);
        
        if(getlabel(distances(1:k,:)) == 3)
            counts(4) = counts(4) + 1;
        end
    end
    counts(11) = counts(4) + counts(11);
    counts(4) = (1 - counts(4)/size(three_t,1));

    for i=1:size(four_t,1)
        distances(:,1) = training(:,1);
        distances(:,2) = distance(training(:,2:end), four_t(i,:));
        distances = sortrows(distances,2);
        
        if(getlabel(distances(1:k,:)) == 4)
            counts(5) = counts(5) + 1;
        end
    end
    counts(11) = counts(5) + counts(11);
    counts(5) = (1 - counts(5)/size(four_t,1));

    for i=1:size(five_t,1)
        distances(:,1) = training(:,1);
        distances(:,2) = distance(training(:,2:end), five_t(i,:));
        distances = sortrows(distances,2);
        
        if(getlabel(distances(1:k,:)) == 5)
            counts(6) = counts(6) + 1;
        end
    end
    counts(11) = counts(6) + counts(11);
    counts(6) = (1 - counts(6)/size(five_t,1));

    for i=1:size(six_t,1)
        distances(:,1) = training(:,1);
        distances(:,2) = distance(training(:,2:end), six_t(i,:));
        distances = sortrows(distances,2);
        
        if(getlabel(distances(1:k,:)) == 6)
            counts(7) = counts(7) + 1;
        end
    end
    counts(11) = counts(7) + counts(11);
    counts(7) = (1 - counts(7)/size(six_t,1));

    for i=1:size(seven_t,1)
        distances(:,1) = training(:,1);
        distances(:,2) = distance(training(:,2:end), seven_t(i,:));
        distances = sortrows(distances,2);
        
        if(getlabel(distances(1:k,:)) == 7)
            counts(8) = counts(8) + 1;
        end
    end
    counts(11) = counts(8) + counts(11);
    counts(8) = (1 - counts(8)/size(seven_t,1));

    for i=1:size(eight_t,1)
        distances(:,1) = training(:,1);
        distances(:,2) = distance(training(:,2:end), eight_t(i,:));
        distances = sortrows(distances,2);
        
        if(getlabel(distances(1:k,:)) == 8)
            counts(9) = counts(9) + 1;
        end
    end
    counts(11) = counts(9) + counts(11);
    counts(9) = (1 - counts(9)/size(eight_t,1));

    for i=1:size(nine_t,1)
        distances(:,1) = training(:,1);
        distances(:,2) = distance(training(:,2:end), nine_t(i,:));
        distances = sortrows(distances,2);
        
        if(getlabel(distances(1:k,:)) == 9)
            counts(10) = counts(10) + 1;
        end
    end
    counts(11) = counts(10) + counts(11);
    counts(10) = (1 - counts(10)/size(nine_t,1));
    
    counts(11) = (1 - counts(11)/size(test,1));
end
